<template>
    <div class="group-set">
        <div v-if="isGroupManager === false">
            <span>暂无操作选项，敬请期待</span>
        </div>
        <div v-if="currentItem.userRole === 1 && isGroupManager === true">
            <div v-if="currentItem.isTalk === 0">
                <el-button type="text" @click="updateTalkStatus()">解除禁言</el-button>
            </div>
            <div v-else-if="currentItem.isTalk === 1">
                <el-button type="text" @click="updateTalkStatus()">禁言</el-button>
            </div>
            <el-button type="text" @click="deleteGroupUser()">从本群中删除</el-button>
        </div>
        <div v-if="currentItem.userRole === 0 && isGroupManager === true">
            <el-button type="text" @click="deleteAllGroupUser()">解散群聊</el-button>
        </div>
    </div>
</template>

<script>

    import {updateTalkStatus,deleteAllGroupUser,deleteGroupUser,isOrNotGroupManager} from "_a/userlist";
    import {mapGetters} from "vuex";

    export default {
        name: 'GroupSet',
        props:{
            currentItem:{
                type:Object
            },
            usersList:{
                type:Array
            }
        },
        created() {
            //查询登录账号是否为此群群主
            this.isOrNotGroupManager()
        },
        data() {
            return {
                isGroupManager: false
            }
        },
        computed: {
            ...mapGetters(['userId', 'username', 'phone'])
        },
        methods: {
            updateTalkStatus() {//在群组中禁言某个用户、解除禁言
                if(this.currentItem.isTalk == 1){
                    this.currentItem.isTalk = 0
                }else{
                    this.currentItem.isTalk = 1
                }
                updateTalkStatus(this.currentItem).then(res => {
                    if(res.data == true){
                        this.$emit('success',false)
                    }else{
                        let message = '解禁/禁言失败'
                        this.$message({
                            type: 'success',
                            message
                        })
                    }
                })
            },
            deleteAllGroupUser() {//删除群组所有成员
                deleteAllGroupUser(this.currentItem.groupId).then(res => {
                    if(true == res.data){
                        this.usersList.splice(0,this.usersList.length)
                        this.$emit('success',false)
                    }else{
                        let message = '解散群聊失败'
                        this.$message({
                            type: 'success',
                            message
                        })
                    }
                })
            },
            deleteGroupUser() {//删除群组成员
                deleteGroupUser(this.currentItem.groupId,this.currentItem.userId).then(res => {
                    if(true == res.data){
                        this.usersList.splice(this.usersList.indexOf(this.currentItem),1)
                        this.$emit('success',false)
                    }else{
                        let message = '删除群组成员失败'
                        this.$message({
                            type: 'success',
                            message
                        })
                    }
                })
            },
            isOrNotGroupManager() {//判断是否为群主
                isOrNotGroupManager(this.currentItem.groupId,this.userId).then(res => {
                    this.isGroupManager= res.data;
                })
            }
        },
        mounted() {

        }
    };
</script>

<style scoped>
    .group-set {

    }
    .switch-pos {
        position: absolute;
        right: 30px;
    }
</style>
